Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Sub Return Value Help

by JediWizard (Deacon)
on Aug 01, 2005 at 15:35 UTC ( [id://479931]=note: print w/replies, xml ) Need Help??


in reply to Sub Return Value Help

I cannot tell you why this doesn't work... but I'd like to offer a few suggestions:

  • Do not use bare word filehandles.
  • use the three argument form of open: open(my $fh, '<', $file)
  • printing "I'll run away now" is not a good error handling technique
  • Do you really mean to be capturing TBROWS and not using the back reference? Perhaps you should see prelre
  • If you are doing an exact match... perhaps you want to use substr instead of a regex. (Would be much faster).
  • You appear to be attempting to use HTML templates.... perhaps you should look at HTML::Template.

They say that time changes things, but you actually have to change them yourself.

—Andy Warhol

Replies are listed 'Best First'.
Re^2: Sub Return Value Help
by Anonymous Monk on Aug 01, 2005 at 16:31 UTC
    Do not use bare word filehandles.
    Bare word filehandles have worked fine since perl1.000, where the way 99% of the code used filehandles until 5.6.0, and are still used in the majority of the code.

    Just stating "don't use bare word filehandles" without any motivation at all doesn't serve anybody.

    use the three argument form of open: open(my $fh, '<', $file)
    Again, no motivation why to not use a construct which has worked fine for almost 20 years. Just because there's a different way of doing things isn't reason enough to critize the old use.

    If you are doing an exact match... perhaps you want to use substr instead of a regex. (Would be much faster).
    Really? How about this benchmark:
    use Benchmark 'cmpthese'; our $orig = "a" x 20; $orig .= "<!-- TBROWS //-->"; $orig .= "a" x 20; $orig .= "<!-- TBROWS //-->"; $orig .= "a" x 20; cmpthese(-1, { 's///' => 'my $c = $orig; $c =~ {<!-- TBROWS //-->}{XXXX}g;', substr => 'my $c = $orig; while ((my $i = index($c, "<!-- TBROWS //-->")) > -1 +) { substr($c, $i, 17) = "XXXX"; }' }); __END__ Rate substr s/// substr 240498/s -- -37% s/// 381869/s 59% --

      Secondly: Your posted code does not even compile.

      Benchmark: running s///, substr, each for at least 1 CPU seconds... runloop unable to compile 'my $c = $orig; $c =~ {<!-- TBROWS //-->}{XX +XX}g;': syntax error at (eval 2) line 1, near "}{XXXX" syntax error at (eval 2) line 1, near "} }" code: sub { for (1 .. 1) { local $_; package main; my $c = $orig; $c = +~ {<!-- TBROWS //-->}{XXXX}g;;} } at test_bench.pl line 9

      If you are going to critize, atleast do so in an effective manner.


      They say that time changes things, but you actually have to change them yourself.

      —Andy Warhol

      Bare work file handles are not as safe as lexically scoped scalars. Just because they were used a decade ago doesn't mean we should continue to use them when a more robust alternative is available. Walking everywhere one needed to go worked fine for centuries. Does that mean we shouldn't use cars? What used to be the standard should not always continue. Change can be good. Before perl 5, we used typeglobs to pass around data structures: If you recomend that... you are insane.


      They say that time changes things, but you actually have to change them yourself.

      —Andy Warhol

        Please explain why you believe bareword filehandles are not as safe, and that lexical filehandles are more robust. I can see occasions where the latter might be preferable but I wouldn't necessarily be making an out of hand assertion for all applications.

        /J\

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://479931]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found