Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
tef
May 30, 2004

-> some l-system crap ->
The same sort of people who frown own sort {$a+=$b} @foo

Adbot
ADBOT LOVES YOU

Ursine Catastrophe
Nov 9, 2009

It's a lovely morning in the void and you are a horrible lady-in-waiting.



don't ask how i know

Dinosaur Gum

tef posted:

The same sort of people who frown own sort {$a+=$b} @foo

:psyduck:

Maybe I'm misunderstanding the construct, but wouldn't that always return that $a is larger than $b, unless $b was a sufficiently large negative number to return 0 or less?

I get the feeling there's a side effect I'm missing.

MacGowans Teeth
Aug 13, 2003

OriginalPseudonym posted:

:psyduck:

Maybe I'm misunderstanding the construct, but wouldn't that always return that $a is larger than $b, unless $b was a sufficiently large negative number to return 0 or less?

I get the feeling there's a side effect I'm missing.

Yeah, I don't understand what it's doing:
code:
main::(-e:1):   1
  DB<1> @l = (3,4,7,10,1,5)

  DB<2> x sort {$a+=$b} @l
0  5
1  6
2  21
3  17
4  31
5  7
  DB<3> x @l
0  7
1  31
2  17
3  21
4  6
5  5
So it modified the list by adding stuff together. It's easier to see what it's doing here:
code:
  DB<4> @l = (1,2,3)

  DB<5> x sort {$a+=$b} @l
0  3
1  5
2  3
  DB<6> x @l
0  3
1  5
2  3
So it looks like it added 1+2 and stored it in $l[0], added 2+3 and stored it in $l[1] and left the last element alone.

In summary, I have no idea what this means or why you'd do it. :psyduck:

tef
May 30, 2004

-> some l-system crap ->

OriginalPseudonym posted:

:psyduck:

Maybe I'm misunderstanding the construct, but wouldn't that always return that $a is larger than $b, unless $b was a sufficiently large negative number to return 0 or less?

I get the feeling there's a side effect I'm missing.

sort in a void context is a no-op :eng101:

code:
shamhat:~ tef$ cat foo.pl
my @foo = (1,2,3,4,5);
sort {$a+=$b} @foo;
print "@foo\n";


shamhat:~ tef$ perl foo.pl
1 2 3 4 5
shamhat:~ tef$ 
I learned this from an intercal programmer.

Ursine Catastrophe
Nov 9, 2009

It's a lovely morning in the void and you are a horrible lady-in-waiting.



don't ask how i know

Dinosaur Gum

tef posted:

sort in a void context is a no-op :eng101:

code:
shamhat:~ tef$ cat foo.pl
my @foo = (1,2,3,4,5);
sort {$a+=$b} @foo;
print "@foo\n";


shamhat:~ tef$ perl foo.pl
1 2 3 4 5
shamhat:~ tef$ 
I learned this from an intercal programmer.

Oh, void context. I was expecting something like this:

code:
op@majora:~$ cat foo.pl
my @foo = (1, 2, 3, 4, 5);
@bar = sort {$a += $b} @foo;
print "Item: $_ \n" for @bar;

op@majora:~$ perl foo.pl
Item: 5 
Item: 9 
Item: 7 
Item: 13 
Item: 3 
op@majora:~$
TIL $a and $b are references. :eng101: I just don't know why I'd ever want to do anything with them by reference in a sort. :eng99:

Hughmoris
Apr 21, 2007
Let's go to the abyss!
I tackled Project Euler, Problem 13: http://projecteuler.net/problem=13

Can someone give me advice on how I could have handled this more efficiently? Bear in mind, I've been messing with Perl for about a week now.


code:

#!/usr/bin/perl
use 5.010;
use warnings;
use strict;

my $line;
my $total;
my @newtotal;
my @slicetotal;


open (myinputfile, "numbers.txt");
while(<myinputfile>)
{
	my($line) = $_;
	chomp($line);
	$total += $line;
}

@newtotal = split(//, $total);
splice(@newtotal, 1, 1);
@slicetotal = @newtotal[0..9];
print join('', @slicetotal);

The part towards the bottom feels goofy but I was't sure how else to take the first 10 digits of a really long number.

Roseo
Jun 1, 2000
Forum Veteran

Hughmoris posted:

I tackled Project Euler, Problem 13: http://projecteuler.net/problem=13

Can someone give me advice on how I could have handled this more efficiently? Bear in mind, I've been messing with Perl for about a week now.

The part towards the bottom feels goofy but I was't sure how else to take the first 10 digits of a really long number.

Here's how I'd do it, with some notes below.

code:
#!/usr/bin/perl
use 5.010;
use warnings;
use strict;

use Math::BigInt;

my $total = Math::BigInt->new(0);

open(my $inputfile, '<', "numbers.txt") or die "Unable to open numbers.txt: $!";
while(my $line = <$inputfile>) {
	$total += $line;
}

say 'The first 10 digits are "', substr($total, 0, 10), '"';
Don't predeclare variables
Three argument open
Filehandle as a scalar, not as a bareword
Always check return value of file operations for success (or 'use autodie')
Assign line to scalar on loop line
Don't need to chomp in this case, newlines'll implicitly get stripped during the string->number conversion
Use Math::BigInt so you don't get the scientific exponentation conversion
Use say so you get newlines at the end of your output
Treat the number as a string and substring it

Mithaldu
Sep 25, 2007

Let's cuddle. :3:

Hughmoris posted:

Bear in mind, I've been messing with Perl for about a week now.
Man, you're still using bareword filehandles. :( (And 2-arg open.)

Also, everything ^^^^ said. Look at every single sentence, if you don't get it, research and/or ask. But make no mistake, these things are important.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Roseo posted:

Here's how I'd do it, with some notes below.

code:
#!/usr/bin/perl
use 5.010;
use warnings;
use strict;

use Math::BigInt;

my $total = Math::BigInt->new(0);

open(my $inputfile, '<', "numbers.txt") or die "Unable to open numbers.txt: $!";
while(my $line = <$inputfile>) {
	$total += $line;
}

say 'The first 10 digits are "', substr($total, 0, 10), '"';
Don't predeclare variables
Three argument open
Filehandle as a scalar, not as a bareword
Always check return value of file operations for success (or 'use autodie')
Assign line to scalar on loop line
Don't need to chomp in this case, newlines'll implicitly get stripped during the string->number conversion
Use Math::BigInt so you don't get the scientific exponentation conversion
Use say so you get newlines at the end of your output
Treat the number as a string and substring it

Thanks for taking the time to type all that out, I'll definitely take a look.

Ninja Rope
Oct 22, 2005

Wee.
Can you expand on "don't pre-declare variables"? Ever? Or just in this instance?

For variables used a bunch of times throughout a function, I find it better to pre-declare them.

Anaconda Rifle
Mar 23, 2007

Yam Slacker

Ninja Rope posted:

Can you expand on "don't pre-declare variables"? Ever? Or just in this instance?

For variables used a bunch of times throughout a function, I find it better to pre-declare them.

He meant don't declare them all at the beginning of the script. It's more Perlish to declare them right before you use them, and definitely declare them in the smallest scope you need them. Consider:

code:
my $i;
while ( $some_condition ) {
    for ( $i = 0; $i < 1000; $i++ ) {
        do_something_with($i);
    }
}
Unless you really want $i to be 1000 outside of your while loop, you probably want to declare it as part of the for loop.

Mithaldu
Sep 25, 2007

Let's cuddle. :3:

Ninja Rope posted:

Can you expand on "don't pre-declare variables"? Ever? Or just in this instance?
Same reason you don't want to use globals. The more places a variable can be used in, the more likely you are to get unintended side-effects. Also: If variables are declared as late as possible, it becomes much easier to see which parts of a function are self-contained and can be factored out into their own function, and that is a Good Thing.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Since the OP is over 4 years old, I have to ask. Is 'Learning Perl' still the go to guide for inexperienced programmers trying to learn the language? I've peeked at 'Modern Perl' but it looks a little sparse for a rookie such as myself.

Mithaldu
Sep 25, 2007

Let's cuddle. :3:

Hughmoris posted:

Since the OP is over 4 years old, I have to ask. Is 'Learning Perl' still the go to guide for inexperienced programmers trying to learn the language? I've peeked at 'Modern Perl' but it looks a little sparse for a rookie such as myself.

That's way too old. Try looking on http://perl-tutorial.org

MacGowans Teeth
Aug 13, 2003

Mithaldu posted:

That's way too old. Try looking on http://perl-tutorial.org

If you're talking about the O'Reilly book, the 6th edition just came out, and that covers 5.14, so it's at least up-to-date, though I haven't read it, so I can't vouch for whether it's good or not (I thought the older edition was okay).

Mithaldu
Sep 25, 2007

Let's cuddle. :3:

Sizzler Manager posted:

If you're talking about the O'Reilly book, the 6th edition just came out, and that covers 5.14, so it's at least up-to-date, though I haven't read it, so I can't vouch for whether it's good or not (I thought the older edition was okay).

I was talking about the "Learning Perl" linked in the OP, which was from 2001.

Also, simple test to see if any learning resource is any good:

Does the first actual Perl code example contain strict and warnings? Y/N

The Gripper
Sep 14, 2004
i am winner

Hughmoris posted:

Since the OP is over 4 years old, I have to ask. Is 'Learning Perl' still the go to guide for inexperienced programmers trying to learn the language? I've peeked at 'Modern Perl' but it looks a little sparse for a rookie such as myself.
I picked up the Learning Perl 5th Ed. book earlier this year (updated for 5.10) and with no prior knowledge of Perl other than:
code:
("@ , There's been
°!&  0 an explosion
#^a /|\ at the ASCII
`;<  |  factory!!!!
@a  / \
managed to pick the language up really, really quickly. I read through Modern Perl after that since it came highly recommended AND it's distributed free as a PDF and is updated for 5.14 (http://onyxneon.com/books/modern_perl/index.html#why_free), and that filled the gaps that Learning Perl glossed over without stepping on the toes of anything Learning Perl teaches.

MacGowans Teeth
Aug 13, 2003

Mithaldu posted:

I was talking about the "Learning Perl" linked in the OP, which was from 2001.

Also, simple test to see if any learning resource is any good:

Does the first actual Perl code example contain strict and warnings? Y/N
Oh, okay. I thought the book was mentioned there, too, and it's the first thing I think of when I hear that title.

That's something I've been meaning to ask - in production code, I always use strict and use warnings, and I don't use bareword filehandles, I use the three-argument open, etc., but in short scripts (5-20 lines or so) that I might use one or more times to do something for myself, I generally don't bother with any of those things, because, for instance, it's convenient to just use $foo{$_} and create a hash without having to declare it first. Other than the obvious laziness of this, is there any argument against it besides "you might get into the habit of using sloppy code in important programs," which hasn't happened? I mean, you don't use strict and warnings on one-liners, and short scripts seem to me to just be long one-liners.

Mithaldu
Sep 25, 2007

Let's cuddle. :3:

Sizzler Manager posted:

I mean, you don't use strict and warnings on one-liners, and short scripts seem to me to just be long one-liners.
If you're sure they're throw-away one-shots? Sure, go ahead. That's why it's fine to do it with one-liners, because nobody saves and reuses those.

Rohaq
Aug 11, 2006
Quick question about using sigtrap.

I want to change the subroutine used by sigtrap based on where in the program it currently is when it's killed. Or more specifically, I want to pass different arguments based on where the program is - My subroutine adapts based on the number of arguments specified, and uses it to gracefully close down connections, but if I start adding arguments to my sigtrap at the very start of my program, strict is going to throw a fit because the variables specified haven't been defined yet.

Is there a way to redefine sigtrap's parameters halfway through the script? Is it as simple as running the use sigtrap line again?

EDIT:- I found a better way. Rather than using the sigtrap module, I just redefined $SIG{'INT'} and $SIG{'QUIT'}:
code:
use DBI;
use Net::Stomp;

$SIG{'INT'} = sub { &graceful_exit() }; 
$SIG{'QUIT'} = sub { &graceful_exit() };

# Some code...

my $dbh = DBI->connect('blah');
$SIG{'INT'} = sub { &graceful_exit($dbh) }; 
$SIG{'QUIT'} = sub { &graceful_exit($dbh) };

# Other code...

my $stomp = Net::Stomp->new({ hostname => 'blah', port => 123 });
$SIG{'INT'} = sub { &graceful_exit($dbh, $stomp) }; 
$SIG{'QUIT'} = sub { &graceful_exit($dbh, $stomp) };

sub graceful_exit {
  print "Exiting...\n";
  if ( scalar @_ == 2 ) {
    my $dbh = shift(@_);
    my $stomp = shift(@_);
    $dbh->disconnect;   # Disconnect from the DB.
    $stomp->disconnect; # Disconnect from stomp server.
  } elsif (scalar @_ == 1 ) {
    my $dbh = shift(@_);
    $dbh->disconnect;   # Disconnect from the DB.
  }
  # Other cleanup rubbish.
  exit(0);
}

Rohaq fucked around with this message at 17:46 on Dec 6, 2011

stoops
Jun 11, 2001
I have a cgi script that runs and displays a button that links to a text file. When I click on the button, it loads in the browser, displaying the txt data.

Is there an easy way to make the button download the text file, meaning, when the button is clicked, it goes straight to a "Save As" prompt?

I wasn't sure if there was code to do this on the cgi script itself. I've googled and read that I'd have to do something to the header, but I wasn't quite sure how to go about that.

Ursine Catastrophe
Nov 9, 2009

It's a lovely morning in the void and you are a horrible lady-in-waiting.



don't ask how i know

Dinosaur Gum

stoops posted:

I have a cgi script that runs and displays a button that links to a text file. When I click on the button, it loads in the browser, displaying the txt data.

Is there an easy way to make the button download the text file, meaning, when the button is clicked, it goes straight to a "Save As" prompt?

I wasn't sure if there was code to do this on the cgi script itself. I've googled and read that I'd have to do something to the header, but I wasn't quite sure how to go about that.

You can, but it's finicky.

The long answer is that the server identifies the ".txt" extension as a specific MIME type ("text/plain", in this case). That MIME type is sent to the browser, which says "I use this associated program for this MIME type" and reacts accordingly.

To that end, you can modify the MIME type that .txt files identify with (here's a list to choose from), but there isn't necessarily one catchall MIME type that every machine will "open" properly. For example, you could flip it to "application/octet-stream", but then the end browser may attempt to run the text file as an executable after it finishes downloading.

If it's something you want to give a shot, you can use CGI::header to change the header of the current document being returned to the browser. The other option would be to modify Apache's config or .htaccess files to identify every .txt file in a certain directory as something that's not text/plain.

Winkle-Daddy
Mar 10, 2007
I have a general question for you perl gurus. I'm decent in perl right now, but recently got a new job. One of the things I deal with extremely regularly is abuse. I work at a web hosting company and people are always either uploading nefarious files, or getting their sites compromised. Typically there is some base64 encoded strings in the php that will do things like kick off a bunch of UDP calls, or what have you. It's pretty easy to find abuse by catting the access logs at any given time, look for all php files then grep through each one looking for php's base64 decode function.

However, recently we're running into abusive sites where the obfuscated or encoded functions are put outside of the <?php tags, or it's just in HTML. It looks like these are sitting, waiting to be curled from some other location we have control of in order to do *something* (usually more udp noise). The problem with this is, this makes it much harder to find.

I'm trying to write a perl script that can grep through files and find a way to determine if a string of characters is base64 encoded. Right now my ideas are:

- Open a file for reading in Perl
- Count the line length of each line
- If the line is greater then 100 characters count the occurrence of each character
- If the occurrence of certain characters like X,Z,V account for a higher then average percent of characters within the string, then flag it for review.

Anyone else have better ideas? This seems like it would be an incredibly slow job for Perl...especially since we serve content for about 2 million accounts.

Ninja Rope
Oct 22, 2005

Wee.

OriginalPseudonym posted:

You can, but it's finicky.

Or you can try sending a Content-Disposition header.

Rohaq
Aug 11, 2006

Winkle-Daddy posted:

I have a general question for you perl gurus. I'm decent in perl right now, but recently got a new job. One of the things I deal with extremely regularly is abuse. I work at a web hosting company and people are always either uploading nefarious files, or getting their sites compromised. Typically there is some base64 encoded strings in the php that will do things like kick off a bunch of UDP calls, or what have you. It's pretty easy to find abuse by catting the access logs at any given time, look for all php files then grep through each one looking for php's base64 decode function.

However, recently we're running into abusive sites where the obfuscated or encoded functions are put outside of the <?php tags, or it's just in HTML. It looks like these are sitting, waiting to be curled from some other location we have control of in order to do *something* (usually more udp noise). The problem with this is, this makes it much harder to find.

I'm trying to write a perl script that can grep through files and find a way to determine if a string of characters is base64 encoded. Right now my ideas are:

- Open a file for reading in Perl
- Count the line length of each line
- If the line is greater then 100 characters count the occurrence of each character
- If the occurrence of certain characters like X,Z,V account for a higher then average percent of characters within the string, then flag it for review.

Anyone else have better ideas? This seems like it would be an incredibly slow job for Perl...especially since we serve content for about 2 million accounts.
You can use regex to determine if something is base64 encoded, I suppose. Someone else seems to have solved that here: http://stackoverflow.com/questions/475074/regex-to-parse-or-validate-base64-data

Quite how fast it'll be, I have no idea. You could do an initial run and then set something up to scan files as they're uploaded though. It wouldn't help with dynamic pages pulling data from an external source though, but that doesn't seem to be something that you're checking for anyway.

tef
May 30, 2004

-> some l-system crap ->

Winkle-Daddy posted:

Anyone else have better ideas? This seems like it would be an incredibly slow job for Perl...especially since we serve content for about 2 million accounts.

keep backups, run a (trained) spam filter over the diffs :v:

more seriously, it looks like you are trying to write a virus scanner for php.

Winkle-Daddy
Mar 10, 2007

Rohaq posted:

You can use regex to determine if something is base64 encoded, I suppose. Someone else seems to have solved that here: http://stackoverflow.com/questions/475074/regex-to-parse-or-validate-base64-data

Quite how fast it'll be, I have no idea. You could do an initial run and then set something up to scan files as they're uploaded though. It wouldn't help with dynamic pages pulling data from an external source though, but that doesn't seem to be something that you're checking for anyway.

Sweet, this should work and make my life easier! Thanks man!

tef posted:

more seriously, it looks like you are trying to write a virus scanner for php.

Eh, more like a way to determine the probability that certain strings are base64 encoded without looking through all of the files I'm searching manually.

I don't think the solution that Rohaq wrote will be too hard because I won't be actively scanning every user directory, I'll probably just tail the last 10,000 or so lines of the apache logs when monitoring picks up unusual traffic.

tef
May 30, 2004

-> some l-system crap ->

Winkle-Daddy posted:

Eh, more like a way to determine the probability that certain strings are base64 encoded without looking through all of the files I'm searching manually.

Which you are doing for the purposes of finding malicious code.

qntm
Jun 17, 2009

Winkle-Daddy posted:

Sweet, this should work and make my life easier! Thanks man!

You should probably know that words like "you", "should", "probably", "know" and "that" are valid base64 strings, as is the empty string, so you'll want to set a minimum length.

Either way this is technically a purely regular regex, so it should run in linear time.

It won't, but that's Perl's problem.

Rohaq
Aug 11, 2006
So I need to execute a script from within itself, and check to see if it exited with an error or not. I'm currently using backticks, since that waits for the script to finish before continuing the parent script, and apparently can capture the output, however it seems to grab the STDOUT output, rather than the exit code.

Does anybody know a method where I can just capture an exit error code (as in on death, not STDERR output, since it's likely I'll have nonfatal warnings in there which don't necessarily mean that the script failed) from a script being run within another script, so I can see if it completed successfully?

Anaconda Rifle
Mar 23, 2007

Yam Slacker
http://perldoc.perl.org/functions/system.html

perldoc posted:

The return value is the exit status of the program as returned by the wait call. To get the actual exit value, shift right by eight (see below). See also exec. This is not what you want to use to capture the output from a command; for that you should use merely backticks or qx//, as described in `STRING` in perlop. Return value of -1 indicates a failure to start the program or an error of the wait(2) system call (inspect $! for the reason).

qntm
Jun 17, 2009
You can also continue to use backticks, then inspect the value of $?, which is populated with the exit word when the child process finishes.

Rohaq
Aug 11, 2006
Aah, I was under the impression that system didn't return anything, now I realise that people were talking about the fact that you couldn't get the contents of its STDOUT via a system call. Thanks!

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Ok, I need someone to point me in the right direction. I know how to search a string or file for a word and print out that word. What I want to do now is search a file for a word, and if it finds it, print out the entire sentence that contains that word. Whats a good way to approach this?

*For example, the text file is a short story and I want to search for a character's name, and print out every sentence that has the name.

Hughmoris fucked around with this message at 01:08 on Dec 13, 2011

Ursine Catastrophe
Nov 9, 2009

It's a lovely morning in the void and you are a horrible lady-in-waiting.



don't ask how i know

Dinosaur Gum

Hughmoris posted:

Ok, I need someone to point me in the right direction. I know how to search a string or file for a word and print out that word. What I want to do now is search a file for a word, and if it finds it, print out the entire sentence that contains that word. Whats a good way to approach this?

*For example, the text file is a short story and I want to search for a character's name, and print out every sentence that has the name.

code:
for(<>){
  print if /name/
}
Edit: That'll print the *line* that has the character name. It'll be a bit more finicky to do by sentence. For a very quick fix, you could set $/ to "." and it'll read in "lines" using "." as a delimiter.

Ursine Catastrophe fucked around with this message at 01:41 on Dec 13, 2011

Roseo
Jun 1, 2000
Forum Veteran

OriginalPseudonym posted:

code:
for(<>){
  print if /name/
}
Edit: That'll print the *line* that has the character name. It'll be a bit more finicky to do by sentence. For a very quick fix, you could set $/ to "." and it'll read in "lines" using "." as a delimiter.

If you want a very quick fix, you should use CPAN.


code:
use strict;
use warnings;
use 5.012;

use Lingua::Sentence;

my $text = <<EOF;
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In mauris nunc, fermentum at euismod in, congue ut arcu. Vivamus semper, turpis vitae ullamcorper malesuada, nisl leo sodales est, a hendrerit enim turpis id diam. Morbi sit amet nibh quam. Aliquam cursus turpis ac augue dapibus vitae feugiat arcu accumsan. Pellentesque augue quam, vulputate aliquet facilisis a, volutpat ac mauris. Suspendisse et est metus, vitae accumsan mauris. Cras risus quam, adipiscing vitae egestas ut, congue sit amet ante. Praesent Mr. Brown eu elit quam. Vivamus vel quam vel massa imperdiet mollis non non lectus. Morbi consectetur convallis dui, sit amet pharetra odio imperdiet non. Proin in leo at dui vestibulum vehicula. Nullam pellentesque cursus mauris, ut bibendum lorem adipiscing elementum. Pellentesque a dui non elit mollis cursus. Nunc augue nisl, laoreet ac tempus et, imperdiet et 4.56 ipsum. Integer lacus odio, dapibus pretium eleifend non, ullamcorper in mauris.
Curabitur viverra tellus elit, venenatis accumsan turpis. Fusce pulvinar consectetur mi. Aliquam erat volutpat. Nulla tellus arcu, suscipit vitae ultrices vel, aliquam a dolor. Vestibulum non ante non mi porttitor tincidunt. In eu odio nisi. Aliquam erat volutpat. Quisque aliquet, dolor vitae consectetur hendrerit, mi tellus tempor purus, eu venenatis lorem elit eu enim. Proin ut fermentum neque. Nunc neque lorem, malesuada non iaculis eget, iaculis vel enim. Suspendisse neque augue, malesuada a suscipit ac, porttitor vitae sapien. Donec molestie erat at neque cursus quis faucibus metus faucibus. Aliquam erat volutpat. Vivamus hendrerit rutrum tristique. Aenean venenatis nisl in leo ultrices sagittis. Pellentesque sodales auctor metus, id dapibus ligula viverra nec. 
EOF

my $splitter = Lingua::Sentence->new("en");
for ($splitter->split_array($text)) {
  say if /ipsum/i;
}

Anaconda Rifle
Mar 23, 2007

Yam Slacker
Be careful using for(<>). This pre-loads the file into memory. If the file is huge, use while(<>).

Rohaq
Aug 11, 2006
Quick question, I'm using while(<>) to iterate through a file line by line at the moment, is there a quick way to remove the line being processed from the file at the end of the loop, without messing up the while loop? I'd like to process a line, then remove it from the file after it's done.

Anaconda Rifle
Mar 23, 2007

Yam Slacker

Rohaq posted:

Quick question, I'm using while(<>) to iterate through a file line by line at the moment, is there a quick way to remove the line being processed from the file at the end of the loop, without messing up the while loop? I'd like to process a line, then remove it from the file after it's done.

Tie::File, perhaps?

Adbot
ADBOT LOVES YOU

Rohaq
Aug 11, 2006

Anaconda Rifle posted:

Tie::File, perhaps?
Excellent, thanks!

  • Locked thread