Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^4: how to open a data file via http

by ikegami (Patriarch)
on Feb 27, 2007 at 16:39 UTC ( [id://602331]=note: print w/replies, xml ) Need Help??


in reply to Re^3: how to open a data file via http
in thread how to open a data file via http

What is your definition of "doesn't work" here?

The goal was a replacement for <FILE>, so anything that doesn't return what <FILE> is buggy. In hangon's favour, I didn't consider autochomping a bug.

my $data = "\n" # 1 . "abc\n" # 2 . "def\n" # 3 . "\n" # 4 . "\n" # 5 . "ghi\n" # 6 . "\n" # 7 . "\n"; # 8 { print("<FILE>\n"); my $lines = 0; open(my $fh, '<', \$data); while (<$fh>) { ++$lines; chomp; print("[$_]\n"); } print($lines, "\n"); } print("\n"); { print("split(/\n/, \$data)\n"); my $lines = 0; foreach (split(/\n/, $data)) { ++$lines; print("[$_]\n"); } print($lines, "\n"); } print("\n"); { print("split(/\n/, \$data, -1)\n"); my $lines = 0; foreach (split(/\n/, $data, -1)) { ++$lines; print("[$_]\n"); } print($lines, "\n"); } print("\n"); { print("split(/^/m, \$data)\n"); my $lines = 0; foreach (split(/^/m, $data, -1)) { ++$lines; chomp; print("[$_]\n"); } print($lines, "\n"); }

outputs

<FILE> [] [abc] [def] [] [] [ghi] [] [] 8 <-- This is what <FILE> gives. split(/\n/, $data) [] [abc] [def] [] [] [ghi] 6 <-- Not 8. Failed to emulate <FILE>. split(/\n/, $data, -1) [] [abc] [def] [] [] [ghi] [] [] [] <-- Extra line 9 <-- Not 8. Failed to emulate <FILE>. split(/^/m, $data) [] [abc] [def] [] [] [ghi] [] [] 8 <-- Bingo!

wc -l says 8 as well, by the way.

Perl doesn't do this on my system:

uh, yes it does... $VAR4 = '';. Lines aren't *seperated* by newlines, they are *terminated* by them.

Replies are listed 'Best First'.
Re^5: how to open a data file via http
by pKai (Priest) on Feb 27, 2007 at 17:31 UTC
    The goal was a replacement for <FILE>, so anything that doesn't return what <FILE> would is buggy. … I didn't consider autochomping a bug.

    O.K., that clarification gives me enough context where you were heading to.

    An added chomp($data) before the split /\n/,,-1 will also conform to that abstraction of lines.

    Update: Added the missing ,-1

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 09:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found