4

Reference the 2nd to last line in my script. For some reason Perl is not able to access the variable $perlPort how can I fix this? Note: $perlPort is a bash variable location before my perl script

perl -e '
{
    package MyWebServer;
    use HTTP::Server::Simple::CGI;
    use base qw(HTTP::Server::Simple::CGI);
    my %dispatch = (
        "/" => \&resp_hello,
    );


    sub handle_request {
        my $self = shift;
        my $cgi  = shift;
        my $path = $cgi->path_info();
        my $handler = $dispatch{$path};
        if (ref($handler) eq "CODE") {
            print "HTTP/1.0 200 OK\r\n";
            $handler->($cgi);
        } else {
            print "HTTP/1.0 404 Not found\r\n";
            print $cgi->header,
            $cgi->start_html("Not found"),
            $cgi->h1("Not found"),
            $cgi->end_html;
        }
    }


    sub resp_hello {
        my $cgi  = shift;   # CGI.pm object
        return if !ref $cgi;
        my $who = $cgi->param("name");   
        print $cgi->header,
            $cgi->start_html("Hello"),
            $cgi->h1("Hello Perl"),
            $cgi->end_html;
    }
}


my $pid = MyWebServer->new($perlPort)->background();
print "Use 'kill $pid' to stop server.\n";'
2
  • 1
    In the same way you wrote 'kill $pid' why you don't write '$perlPort'? Commented Oct 29, 2016 at 21:08
  • 1
    $perlPort => $ENV{perlPort} Commented Oct 29, 2016 at 21:08

4 Answers 4

5
export perlPort
perl -e '
...
my $pid = MyWebServer->new($ENV{perlPort})->background();
'
Sign up to request clarification or add additional context in comments.

Comments

2

You can use -s switch to pass variables. See http://perldoc.perl.org/perlrun.html

perl -se '
...
my $pid = MyWebBrowser->new($perlPort)->background();
...' -- -perlPort="$perlPort" 

Comments

1

You can still pass command line arguments to your script. Replace $perlPort with $ARGV[0], then call you script as

perl -e $' ...
         my $pid = MyWebServer->new($ARGV[0])->background();
         print "Use \'kill $pid\' to stop server.\n";' "$perlPort"

Note the other problem: You can't include single quotes inside a single-quoted string in bash. You can work around this by using a $'...'-quoted string as the argument to Perl, which can contain escaped single quotes. If your script doesn't need to read from standard input, it would be a better idea to have perl read from a here-document instead.

perl <<'EOF' "$perlPort"
{
package MyWebServer;
use HTTP::Server::Simple::CGI;
...
my $pid = MyWebServer->new($ARGV[0])->background();
print "Use 'kill $pid' to stop server.\n";
EOF

The best idea is to simply use a script file instead of trying to construct the script on the command line.

Comments

0
perl -e '
   ...
my $pid = MyWebServer->new('$perlPort')->background();
   ...

1 Comment

Unquoted parameter expansions are subject to word-splitting and pathname expansion. The intended value (an integer) is safe, but this is fragile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.