How do I use a perl variable name properly within backticks?

前端 未结 3 600
心在旅途
心在旅途 2020-12-30 04:01

I need to execute the following code on a bash shell:

mogrify -resize 800x600 *JPG

Since the width and height are variables, I tried this:<

3条回答
  •  执笔经年
    2020-12-30 04:08

    This problem isn't specific to backticks. The problem can happen whenever you interpolate Perl variables in a string.

    my $wid = 800;
    my $hit = 600;
    
    print "$widx$hit"; # this has the same problem
    

    The problem is that the Perl compiler can't know where the variable name ("wid") ends and the fixed bit of the string ("x") starts. So it assumes that it is looking for a variable called $widx - which doesn't exist.

    The solution is to put the name of the variable in { ... }.

    my $wid = 800;
    my $hit = 600;
    
    print "${wid}x$hit"; # This works
    

提交回复
热议问题