How to do multiline input in phpsh? - phpsh

Can I something like:
php>multline start;
.... $a =<<< EOF
.... hello hi how are you this is phpSH
.... hello there its test line
.... EOF;
.... multiline end;
in phpsh?

Let's take an example, if multiline statement:
Type: if (condition) {
Just hit Enter
... and write what you want.
} Close the multiline statement. And hit Enter to execute it.
You can further cancel writing by hitting ctrl + c.

Maybe just like this:
php> $test = "
... hello hi how are you this is phpSH
... hello there its test line
... "
<the_empty_result>
php>
php> echo $test
hello hi how are you this is phpSH
hello there its test line
php>
php> =$test
"\nhello hi how are you this is phpSH\nhello there its test line\n"
or
php> $test = "hello hi how are you this is phpSH
... hello there its test line"
<the_empty_result>
php>
php> echo $test
hello hi how are you this is phpSH
hello there its test line
php>
php> =$test
"hello hi how are you this is phpSH\nhello there its test line"
Of course, press Enter to create the above multiline input.

Related

Pass variable from bash script to HTML

I have a bash script that contains a variable. In the same script, I have an HTML section that, for now, outputs "Hello".
I'd like the HTML section of the bash script to retrieve a variable, and display that in the same heading as the "Hello" section. Essentially, the output on the HTML page should be something like this "Hello Adam".
For now, as simple as it gets, my bash script code is as follows:
#!/bin/bash
firstname="Adam"
echo $firstname
cat << _EOF_
<!DOCTYPE html>
<html>
<body>
<h1>Hello</h1>
</body>
</html>
_EOF_
How do I get the variable in the HTML section so that it can display the $firstname variable?
For reference, I currently run the script using git-bash by executing sh {name-of-script} > test.html
You can use regular variable expansion in unquoted here documents:
# Hello Adam
firstname="Adam"
cat << EOF
Hello $firstname
EOF
If you want certain values to not be expanded, escape them:
# $firstname is Adam
cat << EOF
\$firstname is $firstname
EOF
If you don't want any values to be expanded, quote the delimiter:
# $firstname is not expanded in quoted heredocs
cat << "EOF"
$firstname is not expanded in quoted heredocs
EOF

Storing "JSON to HASH" output in a variable makes Data::Dumper not work in perl

I´m getting an issue when trying to store a json comming from a String into a Hash. Take a look at this example:
use strict;
use warnings;
use utf8;
use JSON;
use Data::Dumper;
my %hash1 = %{get_hash_from_json()};
print "Final Dump:\n";
print Dumper \%hash1 . "\n";
print "Keys:\n";
for (keys %hash1) {printf "key is $_\n";}
sub get_hash_from_json (){
my $json = '{"hello": "world", "hello2": "world"}';
print "Dumper without variable:\n";
print Dumper (from_json($json)) . "\n";
print "Dumper with variable:\n";
my $hash_ref = from_json($json);
my %hash = %{$hash_ref};
print Dumper \%hash . "\n";
return from_json($json);
}
And the output is:
main::get_hash_from_json() called too early to check prototype at example.pl line 10.
Dumper without variable:
$VAR1 = {
'hello' => 'world',
'hello2' => 'world'
};
Dumper with variable:
$VAR1 = 'HASH(0x29c88e0)
';
Final Dump:
$VAR1 = 'HASH(0x2512438)
';
Keys:
key is hello2
key is hello
Does anyone understand why is this happening? Somehow the hash is there but Data::Dumper won´t take it?
Precedence issue.
print Dumper \%hash . "\n";
means
print(Dumper(\%hash . "\n"));
but you want
print(Dumper(\%hash) . "\n");
The thing is, the value returned by Dumper will already end with a new line, so you really don't need another one. The following would do:
print(Dumper(\%hash));
If you want to omit the parens, there's no harm here.
print Dumper \%hash;
As for the prototype-related warning, you are getting it because a call to a sub with a prototype is encountered by the compiler before a declaration for that sub is encountered. In that situation, the call did not use the prototype, so Perl is letting you know this happened.
The simple solution is to just remove the useless prototype (the () in sub get_hash_from_json ()).
You are falling victim to precedence.
print Dumper \%hash1 . "\n";
The . concatenates \%hash1 and the newline, and that's what Dumper outputs. Put parentheses around it to make it work.
print Dumper(\%hash1) . "\n";
Or use say.

insert a shell script declared variable into HTML heading

I have a variable that am declaring like below in my shell script:
Variable = awk 'Some code' filename.txt << this is assigning one word from the text file to this variable
And then am trying to display this in an HTML heading like below, but its not working.
<h2><font color="navy"> Network-element : ${variable} </font></h2>
any help is appreciated.
I think there could be 2 points here.
1- Cover the variable value like:
Variable=$(awk 'Some code' filename.txt)
2- Then in shell script html code doesn't work like simple print you have to use echo for it (NOTE this is only an example)eg->
echo "<html>" > $OUTPUT_FILE
echo "<title>" >> $OUTPUT_FILE
echo "A Test script." >> $OUTPUT_FILE
echo "</title>" >> $OUTPUT_FILE
echo "</body>" >> $OUTPUT_FILE
echo "</html>" >> $OUTPUT_FILE
This above code is only an example where I am putting echo statements into output file. You could use it as per your use case. Also use "$Variable" in echo command.

perl extract text between html tags using regex

I'm new to Perl and im trying to extract the text between all <li> </li> tags in a string and assign them into an array using regex or split/join.
e.g.
my $string = "<ul>
<li>hello</li>
<li>there</li>
<li>everyone</li>
</ul>";
So that this code...
foreach $value(#array){
print "$value\n";
}
...results in this output:
hello
there
everyone
Note: Do not use regular expressions to parse HTML.
This first option is done using HTML::TreeBuilder, one of many HTML Parsers that is available to use. You can visit the link provided above and read the documentation and see the example's that are given.
use strict;
use warnings;
use HTML::TreeBuilder;
my $str
= "<ul>"
. "<li>hello</li>"
. "<li>there</li>"
. "<li>everyone</li>"
. "</ul>"
;
# Now create a new tree to parse the HTML from String $str
my $tr = HTML::TreeBuilder->new_from_content($str);
# And now find all <li> tags and create an array with the values.
my #lists =
map { $_->content_list }
$tr->find_by_tag_name('li');
# And loop through the array returning our values.
foreach my $val (#lists) {
print $val, "\n";
}
If you decide you want to use a regular expression here (I don't recommend). You could do something like..
my $str
= "<ul>"
. "<li>hello</li>"
. "<li>there</li>"
. "<li>everyone</li>"
. "</ul>"
;
my #matches;
while ($str =~/(?<=<li>)(.*?)(?=<\/li>)/g) {
push #matches, $1;
}
foreach my $m (#matches) {
print $m, "\n";
}
Output:
hello
there
everyone
Note: Do not use regular expressions to parse HTML.
hwnd has already provided one HTML Parser solution.
However, for a more modern HTML Parser based off css selectors, you can check out Mojo::DOM. There is a very informative 8 minute intro video at Mojocast episode 5.
use strict;
use warnings;
use Mojo::DOM;
my $html = do {local $/; <DATA>};
my $dom = Mojo::DOM->new($html);
for my $li ($dom->find('li')->text->each) {
print "$li\n";
}
__DATA__
<ul>
<li>hello</li>
<li>there</li>
<li>everyone</li>
</ul>
Outputs:
hello
there
everyone

Pass block of text from webpage to perl script and then back to web

I am trying to do the following.
Take a block of text that a user inputs in a TEXTAREA FORM from a website and pass it to a perl/cgi script that adds the line number before each line. So for example:
diet coke
potato chips
gelato
would become
1 diet coke
2 potato chips
3 gelato
I know how to pass a single value or a bunch of values to a perl script, but when I try to do a foreach (#array) to add a line number it doesn't work. Wondering how to do this.
My html file is
<HTML>
<BODY>
<FORM ACTION="/cgi-bin/splitfoods.pl">
<P>What did you eat today? <BR><TEXTAREA NAME="value" ID="value" style="width:900px;height\
:700px;background-color:#FFF8DC;font-size:20px">
</TEXTAREA>
<P><INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM>
</BODY>
</HTML>
and the cgi file is (from matthewh)
#!/usr/bin/perl
use CGI;
use CGI qw(:standard);
$query = new CGI;
#foods = split('\n',$query->param("value"));
# -- HTML STUFF --
print header;
print start_html;
for($i=1; $i<=#foods; $i++) {
print "$i #foods[$i-1]";
print "<br>";
}
print end_html;
request looks like
cgi-bin/splitfoods.pl?value=diet+coke%0D%0Apotato+chips%0D%0Agelato
Thanks
#foods = split('\n',$query->param('food'));
for($i=1; $i<=#foods; $i++) {
print "$i #foods[$i-1]\n";
}
This is a bit cleaner and less likely to breakdown on edge cases. Plus the numbering is HTML instead which is more natural for web. You really should read the entire document for CGI and always start code with warnings and strict on.
use strict;
use warnings;
no warnings "uninitialized";
use CGI ":standard";
# Scalar/array context matters with param()!
my $food = param("value");
my #foods = split /\n/, $food;
print
header(),
start_html(),
ol(li( \#foods )),
end_html();