perl doing different things on two different platforms - perl

On Mac OSX, this works fine with perl
perl -v
This is perl, v5.8.9 built for darwin-2level
perl -e 'sub test {}'
But on Solaris
perl -v
This is perl, v5.8.8 built for i86pc-solaris-thread-multi
perl -e 'sub test {}'
Illegal declaration of anonymous subroutine at -e line 1.
Any ideas?
Thanks,
Kelly

It's most likely a behavior difference between the two versions of Perl. It's also probably just a bug in the CLI evaluation mode in 5.8.8
Try this test to see if it's just the CLI evaluation or Perl itself:
use strict;
sub test {}
If it passes strict mode in a file, it's probably as good as it's gonna get.

perldoc perldiag says:
Illegal declaration of anonymous subroutine
(F) When using the sub keyword to construct an anonymous subroutine,
you must always specify a block of code. See perlsub
It's could possibly be in a sitecustomize.pl file. It's not seeing the "test". It's reading it as 'sub'. Try typing perl -e 'test {}' on the command line.
Also to take out the customization file, you could add the -f switch to the command line. `perl -fe 'sub test {}'
perldoc perlrun for more information.

Related

What is the difference between uppercase "-E" switch and lowercase "-e" in perl?

How does the perl switches "-E" and "-e" differ from each other? In this example they works exactly the same — executes the command after the switch:
$ perl -e 'print "$_\n" foreach 1..2'
1
2
$ perl -E 'print "$_\n" foreach 1..2'
1
2
This is explained in perldoc perlrun:
-E commandline
behaves just like -e, except that it implicitly enables all optional features (in the main compilation unit). See feature.
The "See feature." refers to the documentation for the feature pragma, which you can read by typing perldoc feature.
-E unlike -e enables features
You can check what these are using Deparse module (following is for perl 5.16),
perl -MO=Deparse -E 1
use feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
From Perldoc:http://perldoc.perl.org/perlrun.html
•-e commandline :
may be used to enter one line of program. If -e is given, Perl will not look for a filename in the argument list. Multiple -e commands may be given to build up a multi-line script. Make sure to use semicolons where you would in a normal program.
•-E commandline :
behaves just like -e, except that it implicitly enables all optional features (in the main compilation unit). See feature.

executing perl code from commandline in windows

i am very new to perl just began to learn when i try to assign a scalar a value and the print it i am facing problem. I am doing
perl -e "$number=30;"
perl -e "print $number;"
the output doesnt show anything but when i do
perl -e "$number=30; print $number;"
the output shows 30 why?
This:
perl -e "$number=30;"
runs the Perl program $number=30;, which sets the variable $number to 30 and then does nothing with it.
This:
perl -e "print $number;"
runs the Perl program print $number;, which prints the value of the uninitialized variable $number.
The key point is that, since these are two completely separate Perl programs, there's no connection between the variable $number in the first program and the variable $number in the second program. There is no relationship between the two programs, and no communication between them, so they do not and cannot share any variables.
when you execute:
perl -e "$number=30;"
perl -e "print $number;"
these are two independent processes, so they don't share information. Variable $number won't be available to the second command.
The other one:
perl -e "$number=30; print $number;"
Works because it runs in the same execution, and $number is visible to the next print sentence

Output of perl debugger to file (Windows)

I tried to list all the subroutines of a script with the perl debugger and put the results in an external file. But It didn't work.
My code:
perl -d -S myscript.pl > results.txt
-S = list all subroutines
-d = debug perl script
Greets,
The -S isn't supposed to be used as a command line switch. Running perl -d will start a debugger process, and one of the commands you can use there is S.
Example:
$ perl -d tmp/splithttpdconf.pl
Loading DB routines from perl5db.pl version 1.28
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main::(tmp/splithttpdconf.pl:6): my $basedir = shift;
DB<1> S main::
main::BEGIN
main::debug
main::splitconf
DB<2>
In order to get the kind of output you want, you can to use the profiler module Devel::DProf instead. It'll output profiler info into a file which can be read by the dprofpp program. Here's an example to get the list of subroutines:
perl -d:DProf perlscript.pl; dprofpp -T
If you only want the subroutines within your own script, and not those loaded from other modules, add a grep to it, e.g.:
perl -d:DProf perlscript.pl; dprofpp -T | grep main::
Though for the particular question of knowing what subroutines exist in a given program, provided you use a consistent coding style it'd probably be easier to just do a grep "sub.*{" to start with.
In your home directory, create a file called .perldb with the following contents:
parse_options("NonStop=1 LineInfo=results.txt AutoTrace=1 frame=2");
And then run the command
perl -d myscript.pl
If you want to scan and list the entire subroutine's what Perl see's before it runs:
perl -MO=Deparse -f myscript.pl

Difference between "perl" and "perl -w"?

I am learning Perl , very new user . May i know whats the difference between these Perl codes.
#!/usr/bin/perl
&
#!/usr/bin/perl -w
That is not perl code, it's a shebang, which is used in a linux/unix environment as a way to tell the shell what program should be used to run the program file. It has no effect in windows, but it does activate any switches used.
The -w part is a switch for perl, telling it to activate warnings. You can learn more about perl's command line switches by typing perl -h at the command prompt. Read more in perldoc perlrun
-w is the older version of the use warnings pragma, which is preferred nowadays. While -w is global, use warnings is lexical, and can be activated selectively.

What's the point of blessed in Perl?

[root# ~]$ perl -e "print 1 if blessed $a;"
1
[root# ~]$ perl -e "print 1 if blessed $c;"
1
[root# ~]$ perl -e "print 1 if blessed $cee;"
1
It seems always true,the version is 5.8.8.
UPDATE
I'm not running as root, it's CHANGED by me for the sake of privacy:)
blessed is not a keyword in Perl. You are using double quotes in your shell command, so the variables ($a, $c, etc.) are from your shell's environment, they are not Perl variables.
Since these environment variables are probably empty, you are essentially executing the script
print 1 if blessed ;
When used like this, blessed is just a bareword string and always evaluates to true. What you have done is not much difference from running
$ perl -e 'print 1 if foo'
Do you mean blessed from Scalar::Util? You probably want to load the function first:
perl -MScalar::Util=blessed -e "print 1 if blessed $a;"
otherwise your blessed is just bareword (string), which is obviously true.
As has been pointed out, you need to load the module before using the method. Also, if you had used perl -we instead of perl -e, you would probably not have asked this question.
For me, with perl -we, I get this warning:
Can't call method "blessed" without a package or object reference at -e line 1.