Rob Wells
Rob Wells

Reputation: 37103

Why is this intended interpolation interpreted as division by Perl?

G'day,

Why am I getting the following two errors from the script fragment below?

Argument "www4.mh.xxxx.co.uk.logstatsto20090610.gz" isn't numeric in division (/) at line 56

Argument "/logs/xxxx/200906/mcs0.telhc/borg2" isn't numeric in division (/) at line 56

The variables $dir and $log are both strings and the concatenation of the two strings, along with the slash in the middle, is also wrapped with quotation marks.

        foreach my $dir (@log_dirs) {
            foreach my $log (@log_list) {
line 56:        if ( -s "$dir/$log" ) {
                    push(@logs, $dir/$log);
                }
            }
        }

Edit: Line 56 is definitely the if statement. However, Paul, you're right, surrounding the division on line 57 with quotation marks fixes the problem. Thanks.

Edit: The Perl version reporting Line 56 is

stats@fs1:/var/tmp/robertw> /usr/local/perl/bin/perl -v      

This is perl, v5.6.1 built for sun4-solaris

Copyright 1987-2001, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'.  If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.

stats@fs1:/var/tmp/robertw> 

Edit: Though using the method of interpolated strings in Perl, given that the variables are themselves strings and I am attempting to join them together with a slash character isn't the net result string concatenation?

cheers,

Upvotes: 4

Views: 876

Answers (4)

brian d foy
brian d foy

Reputation: 132822

You don't quote the string in your push. Instead of creating paths yourself, try to get into the habit of making portable paths with File::Spec:

use File::Spec::Functions;

my $path = catfile( $dir, $file );

Then you use $path whenever you want that string so you don't have the repeat yourself by remaking the string again (and perhaps doing it wrong the next time ;).

Upvotes: 9

ysth
ysth

Reputation: 98398

I'm curious to know what version of perl you are using? All the ones I can easily try are correctly reporting the line number of the push.

Upvotes: 2

1800 INFORMATION
1800 INFORMATION

Reputation: 135295

It's caused by the following line:

push(@logs, $dir/$log);

You have a division there.

Upvotes: 5

Paul Dixon
Paul Dixon

Reputation: 300865

Line 56 is probably the line after it, where you do try to divide two strings. What your probably intended was

   foreach my $dir (@log_dirs) {
        foreach my $log (@log_list) {
            if ( -s "$dir/$log" ) {
                push(@logs, "$dir/$log");
            }
        }
    }

Upvotes: 12

Related Questions