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?


In reply to help declaring variables within perl one-liner by Special_K

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.