Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Referencing string: Can't use string as a SCALAR ref

by P0w3rK!d (Pilgrim)
on May 12, 2003 at 18:10 UTC ( #257513=perlquestion: print w/replies, xml ) Need Help??

P0w3rK!d has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

Given the error:

Can't use string ("123.xml") as a SCALAR ref while "strict refs" in use at C:\Work\foo.PL line 357.
from this code:
# Note: strFileA = C:\foo\a\b\c\123.xml my @aryFile = split (/\//, $strFileA); my $intSize = @aryFile; $strFileB= \$aryFile[$intSize-1];
$strFileB prints out as 123.xml:
05/12 13:55:10 [DBUG] performing FTP of C:/Work/ftp/123.xml...
but when I call this:
$ftp->put($strFileA, $strFileB) or die "FTP: Couldn't put $strFileB.";
The file on the ftp site says: (0x1df8cb0) ..see below:
(000025) 5/12/2003 2:04:21 PM - perlmonk (123.45.67.186) > STOR SCALAR +(0x1df8cb0) (000025) 5/12/2003 2:04:21 PM - perlmonk (123.45.67.186) > asked to up +load 'SCALAR(0x1df8cb0)' in 'C:\Temp\' --> Access allowed. (000025) 5/12/2003 2:04:21 PM - perlmonk (123.45.67.186) > 150 Opening + data connection for SCALAR(0x1df8cb0). (000025) 5/12/2003 2:04:21 PM - perlmonk (123.45.67.186) > started upl +oading 'SCALAR(0x1df8cb0)' in 'C:\Temp\'. (000025) 5/12/2003 2:04:21 PM - perlmonk (123.45.67.186) > 226 File re +ceived ok. (000025) 5/12/2003 2:04:21 PM - perlmonk (123.45.67.186) > finished up +loading 'SCALAR(0x1df8cb0)' in 'C:\Temp\' - (00:00:01 - 8.345 KB - 8 +.345 KBytes/s).
I tried the hard reference with
\$aryFile[$intSize-1];
but that did not do it.

1. Is there a better way to trim down to the filename?
2. What did I do wrong to get $strFileB?

Thanks. :)

-P0w3rK!d

P.S.: I already checked the archives before posting this question. I did not find an answer.

Replies are listed 'Best First'.
Re: Referencing string: Can't use string as a SCALAR ref
by Limbic~Region (Chancellor) on May 12, 2003 at 18:14 UTC
    P0w3rK!d,
    Ok - completely misread the question the first time. Try taking a look at File::Basename, but here's a RE to try if you are 100% of your filenames and know there will be no gotchas.
    my $strFileA = 'C:\foo\a\b\c\123.xml'; my ($filename) = ($strFileA =~ /\\([^\\]*)$/); print $filename,"\n";

    Cheers - L~R

      Thank you :) I have it working now. I'm just curious if I can do it a better way.

      -P0w3rK!d

        There is a better way to do it. (Which happens to be Limbic~Region's first suggestion.) As an added bonus, it also makes the code more portable to other OS's (as long as you have the script setup a directory or something rather than hard-coding it.)

        use File::Basename; my $file = basename($filestring); print $file,$/;

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1

      $strFileA comes in as a function parameter.
Re: Referencing string: Can't use string as a SCALAR ref
by P0w3rK!d (Pilgrim) on May 12, 2003 at 18:20 UTC
    Found one problem:
    logFunc("...performing FTP of $$strFileB");
    I did not see the extra $. DoH!

    Performing:

    $strFileB = $$strFileB;
    ..did the trick.

    Is there a better way to do this?

    -P0w3rK!d

      I think the reason that started happening is because you did
      \$array[index]
      . I'm about 99% sure that the log you mentioned, logFunc("...performing FTP of $$strFileB");, didn't show up until you did that.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://257513]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2019-12-11 11:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?