pinnacle has asked for the wisdom of the Perl Monks concerning the following question:

I keep getting the error message as updated in TITLE. Please assist !!

Part of code where I am getting error is undermentioned

`cd; pwd > tmp; cp tmp figs/Com/dis/tmp`; my $con = `cat tmp`; if ($con =~ m/^\/(\w+)\/(\w+)$/){ # print "$1,$2"; open(RESULT,'>> /$1/$2/figs/Com/dis/result.xml' or die "c +ouldn't open: $!"); } select(RESULT);

I am getting undermentioned error

Useless use of a constant in void context at ./test.pl line 47. Can't use string (">> /$1/$2/figs/Com/dis") as a symbol ref while "str +ict refs" in use at ./test.pl line 47.

My understanding is somehow it's not taking /$1/$2 in the file path, but I need that what should I do !!

  • Comment on Can't use string (">> /$1/$2/figs/Com/dis") as a symbol ref while "strict refs" in use
  • Select or Download Code

Replies are listed 'Best First'.
Re: Can't use string (">> /$1/$2/figs/Com/dis") as a symbol ref while "strict refs" in use
by CountZero (Bishop) on May 03, 2011 at 06:31 UTC
    '>> /$1/$2/figs/Com/dis/result.xml'

    You are using single quotes around this string and they do not interpolate the variables. Try using double quotes: ">> /$1/$2/figs/Com/dis/result.xml"

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Can't use string (">> /$1/$2/figs/Com/dis") as a symbol ref while "strict refs" in use
by tchrist (Pilgrim) on May 03, 2011 at 04:18 UTC
    Your parens are wrong, for one thing. And I don’t see how the code you have could do that, since you are not opening a filehandle that contains a string. The correct parenning is:
    open(RESULT, ">> somewhere") || die "can't append to somewhere: $!";
    Without seeing more of your code, I can’t say what’s really going on. It looks like you assigned to the global variable $RESULT the string ">> somewhere" and then tried to open that using the single argument form of open. For example:
    $RESULT = ">> somewhere"; open(RESULT) || die "can't append to $RESULT: $!";
    Is that what you did? If so why?

    And what is with the screwy `cat` business, anyway? :(

Re: Can't use string (">> /$1/$2/figs/Com/dis") as a symbol ref while "strict refs" in use
by John M. Dlugosz (Monsignor) on May 03, 2011 at 05:49 UTC
    That's too strange... why would the parser stop in the middle of a quoted string? Methinks you didn't paste the code accurately.

    If you fixed the parens, the $1 is still not seen because you have a single-quoted string. Interpolation works on double-quoted strings.

Re: Can't use string (">> /$1/$2/figs/Com/dis") as a symbol ref while "strict refs" in use
by wind (Priest) on May 03, 2011 at 00:46 UTC

    Use 3 parameter form of open, and watch your parenthesis.

    my $file = "/$1/$2/figs/Com/dis/result.xml"; open RESULT, '>>', $file or die "Can't open $file: $!"; select(RESULT);

    Also note that you should include your select statement within your if, as there's not point in selecting a file handle if you don't actually open it.

      Because the reported error message cannot result from the code that was shown, it does no good telling them to use 3 arguments to open instead of only 2. That is not where the problem lies. Somehow the code shown is not what is really happening. You cannot fix this just by adding another argument. You have to stop using a string in the first argument: there is no other issue here.
        it does no good telling them to use 3 arguments to open instead of only 2

        Yes, the error message received was not immediately diagnosable given the code shown. Therefore, the only action I could see was to lead the OP toward fixing the 4 things obviously wrong with his code, hopefully leading him toward receiving a more meaningful error message.

        There's certainly some good from that, *fingers crossed*