Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Why I can't remove trailing slash

by anaconda_wly (Scribe)
on Apr 19, 2013 at 15:27 UTC ( #1029536=perlquestion: print w/ replies, xml ) Need Help??
anaconda_wly has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use warnings; sub main { my $path = "\\\\127.0.0.1\\c$\\bak\\"; $path =~ s/\\$//; print __FILE__.__LINE__." path $path\n"; return; }
I want to remove the trailing slash in the path but the code print nothing. Why?

Comment on Why I can't remove trailing slash
Download Code
Re: Why I can't remove trailing slash
by daxim (Chaplain) on Apr 19, 2013 at 15:42 UTC
    You neglect to call main. The escaping notation is broken, you specified $path to have the literal string \\127.0.0.1\ak\. Better use single quotes:
    my $path = '\\\\127.0.0.1\\c$\\bak\\';

    or avoid nasty backslash escaping with heredoc:

    my $path = <<'UNC'; \\127.0.0.1\c$\bak\ UNC chomp $path;
Re: Why I can't remove trailing slash
by choroba (Abbot) on Apr 19, 2013 at 15:43 UTC
    Have you run main()?

    Also note that $\ is a variable to be interpolated in double quotes. \b also has a special meaning (chr 8).

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Why I can't remove trailing slash
by kennethk (Monsignor) on Apr 19, 2013 at 15:44 UTC
    Because you never call the main subroutine.
    use strict; use warnings; sub main { my $path = "\\\\127.0.0.1\\c\$\\bak\\"; $path =~ s/\\$//; print __FILE__.__LINE__." path $path\n"; return; } main();
    or even better
    use strict; use warnings; my $path = <<'EOT'; \\127.0.0.1\c$\bak\ EOT $path =~ s/\\$//; printf "%s %s path %s\n", __FILE__, __LINE__, $path;

    Unlike C or Java, you don't need a main subroutine; the script just runs in the order it's written, like a shell script. Note as well that I had to escape your $ since " interpolates - you were inserting $\ into your $path assignment. If you'd actually run your subroutine, you would have gotten a warning.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Why I can't remove trailing slash
by space_monk (Chaplain) on Apr 19, 2013 at 15:46 UTC

    Hmmm...where to start? :-)

    Its not really clear what you're trying to do here. For one thing you didn't execute the main() routine. Perl does not run any method unless you tell it to, unlike C.

    Use single quotes for your path variable. The $\ in it produces an error. Did you really mean to use the record separator variable here?

    If you are editing/manipulating file paths use some of the standard libraries for doing so (e.g. File::Basename).

    If any of my proposed solutions have minor errors, it's because I don't waste my genius on trivial matters. :-P
Re: Why I can't remove trailing slash
by blue_cowdawg (Monsignor) on Apr 19, 2013 at 15:47 UTC
        I want to remove the trailing slash in the path but the code print nothing. Why?

    Added exactly one line to your code and got results. Here's the new code:

    #!/usr/bin/perl -w use strict; use warnings; sub main { my $path = "\\\\127.0.0.1\\c$\\bak\\"; $path =~ s/\\$//; print __FILE__.__LINE__." path $path\n"; return; } main();
    and here are the results:
    $ ./hackslash.pl Use of uninitialized value $\ in concatenation (.) or string at ./hack +slash.pl line 6. ./hackslash.pl8 path \\127.0.0.1\ak
    What I think you really wanted was to add a backslash before the $ since normally that would have been interpoloated resulting in :
    # note the backslash in front of the dollar sign my $path = "\\\\127.0.0.1\\c\$\\bak\\";
    giving you:
    $ ./hackslash.pl ./hackslash.pl8 path \\127.0.0.1\c$\bak

    Creating a sub without calling it will act as a NOOP.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      Thank you all.... Funny question. My head freezed. And all replies wonderful.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-11-27 23:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (190 votes), past polls