Hi. I happened to notice that the Perl "Read File" snippet was using bareword filehandles and doing a lot more work than it has to. The following uses lexical filehandles, 3-argument open (to prevent anything in the filename from being misinterpreted as an open sigil) and a do block to simplify the assignment.
Finally, a third, blank position is provided so you can tab straight to the next line instead of the end of the block.
my $${1:var} = do { local $/; open my $fh, "<", "${2:file}"; <$fh>; }; $3
PS It's worth mentioning that this is backwards compatible to 5.6 which should be more than plenty these days.
On 21 Jan 2008, at 22:55, Michael G Schwern wrote:
PS It's worth mentioning that this is backwards compatible to 5.6 which should be more than plenty these days.
It probably will be for anything running TextMate anyway.
Oh and hello Schwern :)
On Jan 21, 2008, at 4:55 PM, Michael G Schwern wrote:
Hi. I happened to notice that the Perl "Read File" snippet was using bareword filehandles and doing a lot more work than it has to. The following uses lexical filehandles, 3-argument open (to prevent anything in the filename from being misinterpreted as an open sigil) and a do block to simplify the assignment.
Finally, a third, blank position is provided so you can tab straight to the next line instead of the end of the block.
my $${1:var} = do { local $/; open my $fh, "<", "${2:file}"; <\ $fh>; }; $3
The $3 here is actually bad in this case, once you tab to it your still inside the snippet. There is always an implicit $0 at the end of a snippet (unless it is defined elsewhere) that will be the last tab and remove you from the snippet 'mode'.
Michael Sheets wrote:
On Jan 21, 2008, at 4:55 PM, Michael G Schwern wrote:
Hi. I happened to notice that the Perl "Read File" snippet was using bareword filehandles and doing a lot more work than it has to. The following uses lexical filehandles, 3-argument open (to prevent anything in the filename from being misinterpreted as an open sigil) and a do block to simplify the assignment.
Finally, a third, blank position is provided so you can tab straight to the next line instead of the end of the block.
my $${1:var} = do { local $/; open my $fh, "<", "${2:file}"; <$fh>; }; $3
The $3 here is actually bad in this case, once you tab to it your still inside the snippet. There is always an implicit $0 at the end of a snippet (unless it is defined elsewhere) that will be the last tab and remove you from the snippet 'mode'.
I see. Just have to make sure to have a newline at the end of the snippet. I must have been missing it before.