Special_K has asked for the wisdom of the Perl Monks concerning the following question:
I have a file one_liner_wrapper.pl that contains the following:
#!/usr/bin/perl -w use strict; my $num_subs = `perl -w -0777 -pi -e 'my $c=s/foo/bar/g;print STDOUT " +$c\n"' ./foo_test`; printf("num_subs = $num_subs\n");
The file foo_test contains the following:
foo food fool foot
When I try to execute one_line_wrapper.pl, I receive the following error:
> ./one_liner_wrapper.pl Global symbol "$c" requires explicit package name (did you forget to d +eclare "my $c"?) at ./one_liner_wrapper.pl line 4. Global symbol "$c" requires explicit package name (did you forget to d +eclare "my $c"?) at ./one_liner_wrapper.pl line 4. Execution of ./one_liner_wrapper.pl aborted due to compilation errors.
This error also occurs if I omit the "my " in the above code.
Why I am I receiving this error despite the "my $c" that is included at the start of the statement? If I instead change one_liner_wrapper.pl to the following:
#!/usr/bin/perl -w use strict; my $c; my $num_subs = `perl -w -0777 -pi -e '$c=s/foo/bar/g;print STDOUT "$c\ +n"' ./foo_test`; printf("num_subs = $num_subs\n");
The output is:
> ./one_liner_wrapper.pl Use of uninitialized value $c in concatenation (.) or string at ./one_ +liner_wrapper.pl line 5. Use of uninitialized value $c in concatenation (.) or string at ./one_ +liner_wrapper.pl line 5. num_subs =
What is the proper way to declare variables that will be used only within perl one-liners called from another perl script when "use strict" is in effect?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: help declaring variables within perl one-liner
by hippo (Archbishop) on Apr 01, 2021 at 16:39 UTC | |
Re: help declaring variables within perl one-liner
by ikegami (Patriarch) on Apr 01, 2021 at 19:48 UTC | |
by haukex (Archbishop) on Apr 01, 2021 at 19:54 UTC | |
by ikegami (Patriarch) on Apr 01, 2021 at 20:08 UTC | |
Re: help declaring variables within perl one-liner
by haukex (Archbishop) on Apr 01, 2021 at 20:08 UTC | |
by perlfan (Parson) on Apr 02, 2021 at 05:45 UTC | |
by haukex (Archbishop) on Apr 02, 2021 at 08:48 UTC |