Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the keyword `no` actually do in Perl?

Tags:

module

perl

My previous question has solved my problem, but left me with a lack of understanding.

use 5.014;
use warnings;
use Test::More;

# still has carp after no Carp
package Test0 {
    use Carp qw( carp );
    sub new {
        my $class = shift;
        my $self  = {};

        carp 'good';

        bless $self, $class;
        return $self;
    }
    no Carp;
}

my $t0 = Test0->new;

ok( ! $t0->can('carp'), 'cannot carp');

This test does not pass, which means no ... doesn't do what I think it does, which includes unimporting the symbols. I've read perldoc no, but it really seems to be rather unenlightening. Given the results of this code I'd say that it doesn't exactly do what it advertises.

What does no do? When and why should I be using it?

like image 696
xenoterracide Avatar asked Apr 25 '12 18:04

xenoterracide


People also ask

What does @_ mean in Perl?

Using the Parameter Array (@_) Perl lets you pass any number of parameters to a function. The function decides which parameters to use and in what order.

What is the difference between -> and => in Perl?

What is the exact difference between :: and -> in Perl? -> sometimes works where :: does not. -> is used for dereferencing; :: is used for referring to other packages.

What is @_ and $_ in Perl?

General Variables$_ - The default input and pattern-searching space. @_ - Within a subroutine the array @_ contains the parameters passed to that subroutine.


1 Answers

no calls a package's unimport(), whereas use calls import(), both silently skipping the case where no desired sub is found.

However, few packages — indeed, mostly only the pragma modules — support unimport().

like image 141
pilcrow Avatar answered Sep 23 '22 03:09

pilcrow